package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.platform.enhance.repository.ChannelRepository;
import com.alibaba.citrus.cr.platform.enhance.repository.model.DubboResult;
import com.alibaba.citrus.cr.platform.enhance.repository.utils.ResultUtils;
import com.alibaba.cz.base.model.store.ChannelSDO;

import com.epoch.app.bcorder.model.dto.BaselineBatchQueryChannelsRequest;
import com.epoch.app.bcorder.service.BaseDataService;
import org.springframework.stereotype.Component;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/9/16 20:28
 */
@Component
public class ChannelRepositoryImpl implements ChannelRepository {

    @Resource
    BaseDataService baseDataService;

    @Override
    @RepositoryInvoker
    public List<ChannelSDO> batchQueryChannel(List<String> channelIds) {
        BaselineBatchQueryChannelsRequest request = BaselineBatchQueryChannelsRequest.builder()
                .channelIds(channelIds)
                .build();
        DubboResult result = ResultUtils.getDubboPageResultList((Map) baseDataService.baselineBatchQueryChannels(request));
        if(result.getResult() == null){
            return null;
        }
        List<Map<String,Object>> list = (List<Map<String, Object>>)result.getResult();
        List<ChannelSDO> data = list.stream().map( bean -> {
            return channelConverter(bean);
        }).collect(Collectors.toList());
        return data;
    }

    @Override
    @RepositoryInvoker
    public List<ChannelSDO> batchQueryChannelByCodes(List<String> channelCodes) {
        BaselineBatchQueryChannelsRequest request = BaselineBatchQueryChannelsRequest.builder()
                .channelCodes(channelCodes)
                .build();
        DubboResult result = ResultUtils.getDubboPageResultList((Map) baseDataService.baselineBatchQueryChannels(request));
        if(result.getResult() == null){
            return null;
        }
        List<Map<String,Object>> list = (List<Map<String, Object>>)result.getResult();
        List<ChannelSDO> data = list.stream().map( bean -> {
            return channelConverter(bean);
        }).collect(Collectors.toList());
        return data;
    }

    private ChannelSDO channelConverter(Map map){
        ChannelSDO channelSDO = new ChannelSDO();
        channelSDO.setChannelId((String) map.get("channelId"));
        channelSDO.setChannelCode((String) map.get("channelCode"));
        channelSDO.setChannelName((String) map.get("channelName"));
        channelSDO.setDescription((String) map.get("description"));
        channelSDO.setStatus(((Double) map.get("status")).intValue());
        channelSDO.setChannelType(((Double) map.get("channelType")).intValue());
        channelSDO.setVersion(((Double) map.get("version")).intValue());
        channelSDO.setOutCode((String) map.get("outCode"));
        return channelSDO;
    }

}
